<!-- 换电管理 -->
<template>
  <el-form ref="ruleFormRef" :model="ruleForm" inline class="ruleForm">
    <el-form-item label="设备唯一编号：" prop="deviceCode">
      <el-input
        v-model="ruleForm.queryRo.deviceCode"
        style="width: 150px"
        clearable
        placeholder="请输入设备编号"
      />
    </el-form-item>
    <el-form-item label="站点名称：" prop="deviceCode">
      <el-input
        v-model="ruleForm.queryRo.locationName"
        style="width: 150px"
        clearable
        placeholder="请输入站点名称"
      />
    </el-form-item>
    <el-form-item label="设备与车辆绑定时间：" >
      <el-date-picker
          v-model="bindtime"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          align="right">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="设备与车辆解绑时间：" >
      <el-date-picker
          v-model="unbindtime"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          align="right">
      </el-date-picker>
    </el-form-item>
   
    <el-form-item label="交易类型：" prop="carDriverType">
      <el-select v-model="ruleForm.queryRo.applyType" clearable style="width: 150px">
        <el-option
                    v-for="item in applyTypeList"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                />
      </el-select>
    </el-form-item>
    <el-form-item label="审核状态：" prop="checkType">
      <el-select v-model="ruleForm.queryRo.checkType" clearable style="width: 150px">
        <el-option
                    v-for="item in checkTypeList"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                />
      </el-select>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="search">查 询</el-button>
    </el-form-item>
  </el-form>

  <el-table :data="tableList" style="width: 100%" v-loading="loading">
<!--    <el-table-column prop="carNumber" label="车牌号" align="center" />-->
    <el-table-column prop="deviceCode" label="设备唯一编号" align="center" />
<!--    <el-table-column prop="deviceCard" label="设备工号" align="center" >-->
<!--      <template #default="scope">-->
<!--        <span >{{ scope.row.deviceCard}}</span>-->
<!--      </template>-->
<!--    </el-table-column>-->
    <el-table-column prop="driverName" label="司机名称" align="center" />
<!--    <el-table-column prop="driverCard" label="司机工号" align="center" />-->
    <el-table-column prop="locationName" label="站点名称" align="center" />
<!--    <el-table-column prop="locationAddress" label="站点地址" align="center">-->
<!--      <template #default="scope">-->
<!--        <span >{{ scope.row.locationAddress}}</span>-->
<!--      </template>-->
<!--    </el-table-column>-->
<!--    <el-table-column prop="carDriverType" label="汽车与司机绑定状态" align="center">-->
<!--      <template #default="scope">-->
<!--        <span >{{ carDriver(scope.row.carDeviceType)}}</span>-->
<!--      </template>-->
<!--    </el-table-column>-->
<!--    <el-table-column prop="carDriverOntime" label="汽车与司机绑定时间" align="center"/>-->
<!--    <el-table-column prop="carDriverOfftime" label="汽车与司机解绑时间" align="center"/>-->
    <el-table-column prop="applyType" label="交易类型" align="center">
      <template #default="scope">
        <span >{{ applyTypeSum(scope.row.applyType)}}</span>
      </template>
    </el-table-column>
    <el-table-column prop="carDeviceOntime" label="汽车与设备绑定时间" align="center"/>
    <el-table-column prop="carDeviceOfftime" label="汽车与设备解绑时间" align="center"/>
    <el-table-column prop="bindElectric" label="绑定时电量 (kwh)" align="center" />
    <el-table-column prop="unbindElectric" label="解绑时电量 (kwh)" align="center" />
    <el-table-column prop="preorderDate" label="申请换电时间" align="center" >
      <template #default="scope">
        <span >{{ scope.row.preorderDate || ''}} {{scope.row.preorderBeginTime || ''}}</span>
      </template>
    </el-table-column>
<!--    <el-table-column prop="applyType" label="申请类型" align="center" >-->
<!--      <template #default="scope">-->
<!--        <span >{{ applyTypeNum(scope.row.applyType)}}</span>-->
<!--      </template>-->
<!--    </el-table-column>-->
    <el-table-column prop="checkType" label="审核状态" align="center" >
      <template #default="scope">
        <span >{{ checkTypeNum(scope.row.checkType)}}</span>
      </template>
    </el-table-column>
    <el-table-column prop="operatePeople" label="操作人员" align="center" />
<!--    <el-table-column prop="operateTime" label="操作时间" align="center" />-->
    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button link type="success" @click="approved(scope.row.id)">通过</el-button>
        <el-button link type="danger" @click="reject(scope.row.id)">驳回</el-button>
        <el-button link @click="openDialog(scope.row)" type="primary">查看详情</el-button>
      </template>
    </el-table-column>
  </el-table>

  <footer class="pagination">
    <el-pagination
      v-model:currentPage="ruleForm.current"
      v-model:page-size="ruleForm.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      small
    />
  </footer>

  <ElectricBindDetailDialog
      v-model:show="showDialog"  :detail="detail"
      @approved="approved"
      @reject="reject"
  ></ElectricBindDetailDialog>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import  ElectricBindDetailDialog from './components/electricBindDetailDialog.vue'
import api from '@/api/apis/electricBindApi'
import {ElMessage, ElMessageBox} from "element-plus";
const ruleForm = reactive({
  queryRo:{
    deviceId:"",
    deviceCode:"",
    locationId:"",
    locationName:"",
    applyType:"",
    checkType:"",
    carDeviceOntime:"",
    carDeviceOfftime:"",

  },
  current: 1,
  pageSize: 50,
  searchCount: true
})
const time=ref<any>([])
const bindtime = ref<any>('')
const unbindtime = ref<any>('')

//总数
const total = ref(0)
//搜索
const search = () => {
  ruleForm.current = 1
  getTableList()
}

const checkTypeNum = (value: number) => {
  let obj: any = {
    0: '待审核',
    1: '通过',
    2: '驳回',
  }
  return obj[value]
}

const applyTypeSum = (value: number) => {
  let obj: any = {
    0: '解绑申请',
    1: '绑定申请',
  }
  return obj[value]
}

const applyTypeList =[
  {name:"解绑申请",value:0},
  {name:"绑定申请",value:1},
]

const checkTypeList =[
  {name:"待审核",value:0},
  {name:"通过",value:1},
  {name:"驳回",value:2},
]


//表格数据
const tableList = ref([])
//加载
const loading = ref(false)
onMounted(()=>{
  getTableList()
})
//获取表格数据
const getTableList = async () => {
  loading.value = true
  try {
    let obj = { ...ruleForm }
    // obj.queryRo.carDeviceOntime =time.value?time.value[0]:undefined
    // obj.queryRo.carDeviceOfftime =time.value?time.value[1]:undefined
    obj.queryRo.carDeviceOntime =bindtime.value?bindtime.value:undefined
    obj.queryRo.carDeviceOfftime =unbindtime.value?unbindtime.value:undefined
    let {data} = await api.electricBindPage(obj)
    tableList.value= data.data.rows
    total.value= Number(data.data.total)

  } catch (error) {
    loading.value = false
  }
  loading.value = false
}
// //改变每页条数
const handleSizeChange = (val: number) => {
  ruleForm.current = 1
  ruleForm.pageSize = val
  getTableList()
}
//改变当前页数
const handleCurrentChange = (val: number) => {
  ruleForm.current = val
  getTableList()
}

//弹框开关
const showDialog = ref(false)
//详情id
const detail = ref()
//打开详情弹框
const openDialog = (val: string) => {
  detail.value = val
  showDialog.value = true
}

//审核
const approved = async(id:string) => {
  // console.log("daaaaaaaaaaaaaaaaaaaaaaaa",data)
  ElMessageBox.confirm('是否通过该申请?', '通过操作确认', {
        type: 'warning',
      }
  )
      .then(async() => {
        let { data } = await api.approvedElectric({ id, status:1 })
        if (data.code == 200) {
          getTableList()
          ElMessage.success("审核通过")
          showDialog.value = false
        }
        if(data.code == 1011){
          ElMessage({
            message: '已经审核，请勿重复操作!',
            type: 'error',
          })
          getTableList()
          showDialog.value = false
        }
      })
      .catch(() => {})
}
//驳回审核
const reject = (id:string) => {
  ElMessageBox.confirm('是否驳回该申请?', '驳回操作确认', {
    type: 'warning',
  })
      .then(async() => {
        let { data } = await api.approvedElectric({ id, status:0 })
        if (data.code == 200) {
          getTableList()
          ElMessage.success("已驳回审核")
          showDialog.value = false
        }
        if(data.code == 1011){
          ElMessage({
            message: '已经审核，请勿重复操作!',
            type: 'error',
          })
          getTableList()
          showDialog.value = false
        }
      })
      .catch(() => {})
}
</script>

<style scoped lang="scss">
footer {
  display: flex;
  justify-content: center;
  margin-top: 15px;
}
</style>
